import java.io.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class MusicCollection 
{
   private MusicInit[] collection;
   private int count;
   private String restr[]=new String[4];

   public MusicCollection ()
   {
      collection = new MusicInit[500];
      count = 0;      
   }
   

   public void addData (String title, String name, String cd, String genre)
   {
	   if (count == collection.length)
	         increaseSize();
      collection[count] = new MusicInit(title,name,cd,genre );
      count++;
   }
   
   public void increaseSize ()
   {
      MusicInit[] temp = new MusicInit[collection.length * 2];

      for (int MusicInit = 0; MusicInit < collection.length; MusicInit++)
         temp[MusicInit] = collection[MusicInit];

      collection = temp;
   }

   public void modify (int row, String title, String name, String cd, String genre)
	 {
		 collection[row].newData(title, name, cd, genre);		 
	 }	 
	 

   
   public void delete (int row, String title, String name, String cd, String genre)
	 {
		 collection[row].DelData(title, name, cd, genre);		 
	 }	 
   
   public void Search(String str1, String str2[])
    {
   		 
	   for(int i = 0; i<count; i++)
   		{
   			if(str1.compareToIgnoreCase(collection[i].takeTitle())== 0)
				str2[i] = Integer.toString(i);
			else if(str1.compareToIgnoreCase(collection[i].takeArtist())== 0)
				str2[i] = Integer.toString(i);
			else if(str1.compareToIgnoreCase(collection[i].takeCd())== 0)
				str2[i] = Integer.toString(i);
			else if(str1.compareToIgnoreCase(collection[i].takeGenre())== 0)
				str2[i] = Integer.toString(i);
		}
			
   	 }
  
   public String[] getData(int i)
	 {	 	
	 	 restr[0] = collection[i].takeTitle();
	 	 restr[1] = collection[i].takeArtist();
	 	 restr[2] = collection[i].takeCd();
	 	 restr[3] = collection[i].takeGenre();
	  	 return  restr;
	 }
	   

   public void save()
	 {
		   String save_str="";
		   File file=null;
		   file=new File("music.txt");
		   
	       for(int i=0; i<count; i++)
		   {
	    	   save_str+=collection[i].getSavetxt()+"\n";
		   }
	      
	       JFileChooser ch = new JFileChooser();
	       ch.setCurrentDirectory(new File("."));
	       ch.setSelectedFile(file);
	       		
	       int status = ch.showSaveDialog (null);
		
	       if (status == JFileChooser.CANCEL_OPTION)
	       {
	    	   return ;
	       }
	       else if (status == JFileChooser.APPROVE_OPTION)
	       {
	    	   file=ch.getSelectedFile();
	    	   if(file.exists())
	    	   {
	    		  int response = JOptionPane.showConfirmDialog(null,"Overwrite existing file?","Confirm Overwrite",
 JOptionPane.OK_CANCEL_OPTION,
	    		   JOptionPane.QUESTION_MESSAGE);
	    	   if(response == JOptionPane.CANCEL_OPTION)
	    		   return ;
	    	   }
	    	   writeFile(file, save_str);
	       }
	       else
	       {
	    	   return;
	       }		 
	 }

 public static boolean writeFile (File file, String dataString) 
	 {
	   try 
	   {
		PrintWriter out = new PrintWriter (new BufferedWriter (new FileWriter (file)));
        out.print (dataString);
        out.flush ();
        out.close ();
	   }
	   
	   catch (IOException e) 
	   {
	     return false;
	   }
	  return true;
	   }
	 
	 public void load()
	 {
		count = 0;		
	 }
	 
}

